package com.fizy.client.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.fizy.client.enums.Operation;
import com.fizy.client.methods.FizyMethods;
import com.fizy.client.objects.Album;
import com.fizy.client.objects.Artist;
import com.fizy.client.objects.Search;
import com.fizy.client.objects.Track;
import com.fizy.client.objects.Track2;
import com.fizy.client.utils.MyURLEncoder;

public class FizyControllers {

	// public static void main(String[] args) {
	// FizyControllers controllers = new FizyControllers();
	// Search search = controllers.search("Alişan");
	// // ekrana track ları basmak icin
	// System.out.println("::TRACK::");
	// for (Track track : search.getTracks()) {
	// System.out.println("body :" + track.getBody());
	// System.out.println("name :" + track.getName());
	// }
	//
	// // ekrana album ları basmak icin
	// System.out.println("::ALBUM::");
	// for (Album album : search.getAlbums()) {
	// System.out.println("name :" + album.getName());
	// System.out.println("cover large image :" + album.getCover_large());
	// }
	// // ekrana artist ları basmak icin
	// System.out.println("::ARTIST::");
	// for (Artist artist : search.getArtists()) {
	// System.out.println("name :" + artist.getName());
	// System.out.println("birth date :" + artist.getBirth_date());
	// }
	//
	// // ekrana mood ları basmak icin
	// System.out.println("::MOOD::");
	// for (Mood mood : search.getMoods()) {
	// System.out.println("title :" + mood.getTitle());
	// System.out.println("emotions :" + mood.getEmotions());
	// }
	//
	// // secilen 3. track bilgilerini gostermek icin //track.get ile ws den donem daha fazla bilgiye ulasabilirsin
	// Track2 track = controllers.getTrack(search.getTracks().get(3).getId() + "");
	// System.out.println("name :" + track.getName());
	// System.out.println("url :" + track.getSecure_url());
	//
	// // secilen 5. album bilgilerini gostermek icin //album.get ile ws den donem daha fazla bilgiye ulasabilirsin
	// Album album = controllers.getAlbum(search.getAlbums().get(5).getId() + "");
	// System.out.println("name :" + album.getName());
	// System.out.println("cover large image :" + album.getCover_large());
	//
	// // secilen 2. album bilgilerini gostermek icin //artist.get ile ws den donem daha fazla bilgiye ulasabilirsin
	// Artist artist = controllers.getArtist(search.getArtists().get(2).getId() + "");
	// System.out.println("name :" + artist.getName());
	// System.out.println("birth date :" + artist.getBirth_date());

	// // -----------------------------ALBUM'UN TRACKLARI
	// System.out.println("::ALBUM'UN TRACKLARI::");
	// Album album2 = controllers.getAlbumTracks("304455");
	// for (Track2 track2 : album2.getTracks()) {
	// System.out.println("id :" + track2.getId());
	// System.out.println("name :" + track2.getName());
	// System.out.println("name :" + track2.isIs_listenable());
	// }
	//
	// List<Album> albums = controllers.getArtistAlbums("559995"); // data gelmiyor
	// // List<Album> albums = controllers.getArtistAlbums("522787"); // data geliyor
	// System.out.println("::ARTIS IN ALBUMLERI::");
	// for (Album album : albums) {
	// System.out.println("id : " + album.getId());
	// System.out.println("name :" + album.getName());
	// System.out.println("cover large image :" + album.getCover_large());
	// }
	// }

	public Search search(String searchKey) {
		Search search = null;
		try {
			FizyMethods fizyMethods = new FizyMethods();
			String result = fizyMethods.call(Operation.SEARCH, MyURLEncoder.encode(searchKey));

			// System.out.println("Output from Server : " + result);
			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(result);

			Object n = jsonObject.get("results");
			// System.out.println(n);

			ObjectMapper mapper = new ObjectMapper();
			try {
				search = mapper.readValue(n.toString(), Search.class);
				// album
				List<Album> albums = new ArrayList<Album>();
				for (Album album : search.getAlbums()) {
					if (album.getName().toLowerCase().contains(searchKey.toLowerCase())) {
						albums.add(album);
					}
				}
				search.setAlbums(albums);
				// artist
				List<Artist> artists = new ArrayList<Artist>();
				for (Artist artist : search.getArtists()) {
					if (artist.getName().toLowerCase().contains(searchKey.toLowerCase())) {
						artists.add(artist);
					}
				}
				search.setArtists(artists);
				// track
				List<Track> tracks = new ArrayList<Track>();
				for (Track track : search.getTracks()) {
					if (track.getName().toLowerCase().contains(searchKey.toLowerCase())) {
						tracks.add(track);
					}
				}
				search.setTracks(tracks);

				// System.out.println(search);
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return search;
	}

	public Track2 getTrack(String id) {
		Track2 track = null;
		try {
			FizyMethods fizyMethods = new FizyMethods();
			id += "/?fields=album,artist,secure_url";
			String result = fizyMethods.call(Operation.TRACKS_ID, id);

			// System.out.println("Output from Server : " + result);
			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(result);

			try {
				Object n = jsonObject.get("track");
				// System.out.println(n);
				ObjectMapper mapper = new ObjectMapper();
				track = mapper.readValue(n.toString(), Track2.class);
				// System.out.println(track);
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return track;
	}

	public Album getAlbum(String id) {
		Album album = null;
		try {
			FizyMethods fizyMethods = new FizyMethods();
			// id += "/?fields=artist,cover_large,cover_medium,cover_small";
			String result = fizyMethods.call(Operation.ALBUM_ID, id);

			System.out.println("Output from Server : " + result);
			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(result);

			try {
				Object n = jsonObject.get("album");
				// System.out.println(n);
				ObjectMapper mapper = new ObjectMapper();
				album = mapper.readValue(n.toString(), Album.class);
				// System.out.println(album);
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return album;
	}

	public Artist getArtist(String id) {
		Artist artist = null;
		try {
			FizyMethods fizyMethods = new FizyMethods();
			// id += "/?fields=artist,cover_large,cover_medium,cover_small";
			String result = fizyMethods.call(Operation.ARTIST_ID, id);

			// System.out.println("Output from Server : " + result);
			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(result);

			try {
				Object n = jsonObject.get("artist");
				// System.out.println(n);
				ObjectMapper mapper = new ObjectMapper();
				artist = mapper.readValue(n.toString(), Artist.class);
				// System.out.println(artist);
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return artist;
	}

	public List<Album> getArtistAlbums(String id) {
		List<Album> albums = null;
		try {
			FizyMethods fizyMethods = new FizyMethods();
			id += "/albums";
			String result = fizyMethods.call(Operation.ARTIST_ID_ALBUM, id);

			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(result);

			try {
				Object n = jsonObject.get("albums");
				JSONArray jsonArray = (JSONArray) n;
				ObjectMapper mapper = new ObjectMapper();
				albums = new ArrayList<Album>();
				for (Object object : jsonArray) {
					Album album = mapper.readValue(object.toString(), Album.class);
					albums.add(album);
				}
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return albums;
	}

	public Album getAlbumTracks(String id) {
		Album album = null;
		try {
			FizyMethods fizyMethods = new FizyMethods();
			id += "/tracks";
			String result = fizyMethods.call(Operation.ALBUM_ID_TRACKS, id);

			// System.out.println("Output from Server : " + result);
			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(result);

			try {
				Object n = jsonObject.get("album");
				// System.out.println(n);
				ObjectMapper mapper = new ObjectMapper();
				album = mapper.readValue(n.toString(), Album.class);
				// System.out.println(artist);
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return album;
	}

}
